package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.widget.StateButton;
import com.ukao.cashregister.widget.StateImageView;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 作废
 * Created by ukaodev on 2017/6/2.
 */

public class CrippledDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;

    @BindView(R.id.cancel)
    StateButton cancel;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.edit_text)
    EditText editText;
    @BindView(R.id.describe_layout)
    LinearLayout describeLayout;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.describe_remark)
    TextView describeRemark;
    private boolean isRemark;
    private View view;
    private String remarkContxt;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            isRemark = getArguments().getBoolean(ARG_PARAM1);
            remarkContxt= getArguments().getString(ARG_PARAM2,"");
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.dialog_crippled_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();

        return view;
    }


    protected void initView() {

        if (convertListener != null) {
            convertListener.convertView(ViewHolder.create(view), this);
        }
        if (isRemark) {
            remarkContxt=CheckUtils.isNull(remarkContxt)?"":remarkContxt;
            editText.setText(remarkContxt);
            editText.setSelection(remarkContxt.length());
            title.setText("管理备注");
            describeLayout.setVisibility(View.GONE);
            describeRemark.setVisibility(View.VISIBLE);
            editText.setHint("请输入备注信息");
        } else {
            describeLayout.setVisibility(View.VISIBLE);
            describeRemark.setVisibility(View.GONE);
            title.setText("作废");
        }

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

    private OnDialogSureClickListener mOnDialogSureClickListener;

    public void setOnSureClickListener(OnDialogSureClickListener mOnDialogSureClickListener) {
        this.mOnDialogSureClickListener = mOnDialogSureClickListener;
    }


    @Override
    protected void initListener() {
        super.initListener();


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    protected boolean useEventBus() {
        return false;
    }

    @Subscribe
    public void onEvent(UserInfoEvent event) {

    }

    @OnClick({R.id.cancel, R.id.submit, R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                dismiss();
                break;
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.submit:
                String streditText = getText(editText);
                if (streditText.isEmpty()) return;
                if (mOnDialogSureClickListener != null) {
                    mOnDialogSureClickListener.onSureItemClick(streditText);
                }
                dismiss();
                break;


        }
    }


}
